package com.ssl.order.pay.strategy;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.ssl.order.pojo.OrderSubmitRequest;
import com.ssl.order.utils.Constants;

public class AlipayStrategy implements IPayStrategy{
    @Override
    public String pay(OrderSubmitRequest order) {
        //支付链接的获取
        AlipayClient alipayClient = new DefaultAlipayClient(
                Constants.ALIPAY_GATEWAY,
                Constants.APP_ID,
                Constants.APP_PRIVATE_KEY,
                "JSON",
                "UTF-8",
                Constants.ALIPAY_PUBLIC_KEY,
                Constants.SIGN_TYPE
        );

        AlipayTradePagePayRequest pagePayRequest = new AlipayTradePagePayRequest();
        pagePayRequest.setReturnUrl(Constants.CALLBACK_URL);
        pagePayRequest.setBizContent("{\"out_trade_no\":\"" + order.getOrderId() + "\","
                + "\"total_amount\":\"" + order.getTotalAmount() + "\","
                + "\"subject\":\"" + "商品支付" + "\","
                + "\"body\":\"" + "非公开" + "\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");

        try {
            String payUrl = alipayClient.pageExecute(pagePayRequest, "GET").getBody();
            return payUrl;
        } catch (AlipayApiException e) {
            throw new UnsupportedOperationException("Alipay request failed!" + e);
        }
    }
}
